import {
  getPage
} from '../../utils/config';
import api from '../../utils/api';
import {
  http,
  $loading,
  $toast,
  promisify,
  storage
} from '../../utils/utils';
import uploadImage from '../../utils/aliyunoss/uploadAliyun';
import wxApi from '../../utils/wxApi';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //数据有变化
    getPage: getPage,
    logged: 0,
    month: 0,
    monthJfData:[]
  },

  // 签到
  handleSiginin(e){
    let day = e.detail.day.trim()
    console.log('handleSiginin=',day)
    http.post(api.default, {
      data: {
        action: 'qiandao',
        day:day,
        userid: this.data.userId,
        shopid: this.data.shopId,
      }
    }, false).then(res=>{
      $toast(`签到成功 +${res.data.data}`);

      let {
        localInfo,userInfo
      } = this.data;
      console.log('userInfo===',userInfo)
      let points = userInfo.data.uesr.rank_points + res.data.data;
      // 更新本地存储数据
      this.setData({
        rank_points:points
      })
      localInfo.data.user.rank_points = points;
      storage.set('userInfo', localInfo);
      this.getSiginData()
    })
  },

  // 某一月份签到数据
  getSiginData(){
    http.post(api.default, {
      data: {
        action: 'getqiandaobymonth',
        month:this.data.month,
        shopid: this.data.shopId,
      }
    }, false).then(res=>{
      console.log('ressssssssss',res)
      this.setData({
        monthJfData:res.data.data.month_days,
        monthSigin:res.data.data.list
      })
    })
  },

  // 切换月份
  changeMonth(e){
    let ym = e.detail.ym;
    this.setData({
      month:ym
    })
    this.getSiginData()
  },

  // 获取当前日期
  getNowTime(time, type) {
    var date = time,
      year = date.getFullYear(),
      month = date.getMonth() + 1,
      day = date.getDate(),
      hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
      minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
      second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      month >= 1 && month <= 9 ? (month = "0" + month) : "";
      day >= 0 && day <= 9 ? (day = "0" + day) : "";
      if (type == 1) {
        if(uni.getSystemInfoSync().platform == 'ios'){
          var timer = 
            year + '/' + month + '/' + day + 
            ' ' + hour + ':' + minute + ':' + second;
        }else{
          var timer = year + '-' + month + '-' + day + 
          ' ' + hour + ':' + minute + ':' + second;
        }
      } else {
        var timer = year + '/' + month + '/' + day;
      }
    
    return timer;
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    //获取登录
    app.getLogin().then((res) => {
      this.setData({
        userInfo: res,
        localInfo: res,
        logged: res.code,
        userId: res.data.user.user_id,
        avatar: res.data.user.head_url,
        shopId:res.data.shop.ShopId,
        rank_points:res.data.user.rank_points
      });
      let month = Number(this.getNowTime(new Date(), 2).slice(0, 4))+'-'+Number(this.getNowTime(new Date(), 2).slice(5, 7));
      this.setData({
        month:month
      })
      this.getSiginData()
    }).catch((res) => {
      this.setData({
        logged: res
      });
    });
    //获取system信息
    promisify(wx.getSystemInfo)().then(res => {
      //获取右上角胶囊信息
      this.setData({
        navbar: wx.getMenuButtonBoundingClientRect(),
        window: res,
      })
    });

    //获取路由数量
    this.setData({
      getPages: getCurrentPages().length
    })
  },

  /**
   * 返回上一页
   */
  goBackTap() {
    //存在上一页,关闭当前返回一层,//没有上一页，跳回首页
    this.data.getPages > 1 ? wxApi.navigateBack(1) : wxApi.switchTab(getPage.home);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
})